<?php

namespace Home\Controller;

use Think\Controller;

/**
 * Description of PublicController.class控制器实例
 * 
 * @author songanwei
 * @date 2016-1-4
 */
class PublicController extends Controller {

    /**
     * @description 登录页面视图
     * @modify songanwei 2015-01-04
     */
    public function login() {
        $admin = getAdminId();
        if ($admin) {
            redirect(U('Admin/index'));
            exit;
        }
        $this->display('Admin:login');
    }

    /**
     * @description 退出
     * @modify songanwei 2016-03-17
     */
    public function logout() {
        session(null);
        $this->display('Admin:login');
    }

    /**
     * @description 登录验证
     * @modify songanwei 2016-01-04
     */
    public function checkLogin() {
        $username = I('post.username', '', 'string');
        $password = I('post.password', '', 'string');
        $vcode = I('post.vcode', '', 'string');

        if (!checkVcode($vcode)) {
            returnJson(403, 122, '验证码有误', 'error');
        }
        $bpassLoginModel = D('BpassLogin');
        $bpassLogin = $bpassLoginModel->getInfoByName($username);

        if (empty($bpassLogin)) {
            returnJson(403, 122, '用户名不存在');
        }
        if ($bpassLogin['password'] != $password) {
            returnJson(403, 122, '登录密码不正确');
        }

        $_SESSION[C('ADMIN_ID')] = $bpassLogin['id'];
        $_SESSION[C('ADMIN_NAME')] = $bpassLogin['name'];
        returnJson(200, 200, '登录成功');
    }

    /**
     * @description 获取验证码
     * @modify songanwei 2015-01-04
     */
    public function vcode() {
        $config = array(
            'fontSize' => 14,
            'length' => 4,
            'useNoise' => true,
            'fontttf' => '5.ttf'
        );
        $Verify = new \Think\Verify($config);
        ob_clean();
        $Verify->entry($id);
    }

}
